<template>
	<view>
		<cu-custom bgColor="bg-black" :isBack="false">
			<block slot="right"><text @tap='tomy' style="margin-left: 40rpx; " class="cuIcon-back"></text></block>
			<block slot="content">我的日记</block>
		</cu-custom>
		<view>
			<uni-calendar :insert="true" :lunar="false" :selected="selected" @change="change" @monthSwitch="monthSwitch"></uni-calendar>
		</view>
	</view>
</template>

<script>
	import uniCalendar from '@/components/uni-calendar/uni-calendar.vue'
	import tool from "../../utils/tool.js"
	export default {
		components: {
			uniCalendar
		},
		data() {
			return {
				selected: [],
				selectedmonth: '',
				userInfo: {
					ext_info: {
						yesapi_avatar: "",
						yesapi_email: "",
						yesapi_expiration_time: "",
						yesapi_mobile: "",
						yesapi_nickname: "",
						yesapi_points: 0,
						yesapi_real_name: "",
						yesapi_reg_note: "",
						yesapi_reg_source: "",
						yesapi_sex: ""
					},
					register_ip: "",
					register_time: "",
					role: "",
					rolename: "",
					status: 0,
					username: "",
					uuid: "",
					token: ""
				}


			}
		},
		mounted() {

			var info = this.checkLogin("home", '2');
			if (info) {
				this.userInfo = info;
			}
			let date = new Date();
			let year = date.getFullYear();
			let month = date.getMonth() + 1;
			if (month >= 1 && month <= 9) {
				month = "0" + month;
			}
			if (this.selectedmonth == '') {
				this.selectedmonth = year.toString() + month.toString()
			}
			this.getDays()

		},
		methods: {
			change(e) {

				if (tool.in_array(e.fulldate, this.selected)) {
					uni.redirectTo({
						url: "./detail/detail?date=" + e.fulldate
					})
				} else {
					return;
				}
			},
			monthSwitch(e) {
				if (this.hasContent(e)) {
					let year = e.year;
					let month = e.month;
					if (month >= 1 && month <= 9) {
						month = "0" + month;
					}

					this.selectedmonth = year.toString() + month.toString()

					this.getDays()

				}

			},
			async getDays() {
				var options = {
					data: {
						s: "SVIP.Szskj_MyApi.AListOkayapiselfmoment",
						user_uuid: this.userInfo.uuid,
						moment_month: this.selectedmonth
					},
					method: "POST"
				}
				const res = await this.$myRuquest(options)
				if (res.data.data.err_code == 0) {
					this.selected = res.data.data.items;
				} else {
					uni.showToast({
						icon: '',
						position: 'bottom',
						title: res.data.data.err_msg
					});

				}

			},
			tomy() {
				this.$store.commit("setData", {
					key: "tabpage",
					val: 'my'
				});
				uni.reLaunch({
					url: "../index"
				});
			},

			hasContent(e) {
				let now = new Date()
				let nowyear = now.getFullYear();
				let nowmonth = now.getMonth() + 1;
				if (nowmonth >= 1 && nowmonth <= 9) {
					nowmonth = "0" + nowmonth;
				}
				let nowdate = nowyear.toString() + "-" + nowmonth.toString()
				let tnow = new Date(nowdate);
				let nowtime = tnow.getTime(tnow);

				let changeyear = e.year
				let changemonth = e.month
				if (changemonth >= 1 && changemonth <= 9) {
					changemonth = "0" + changemonth;
				}
				let changedate = changeyear.toString() + "-" + changemonth.toString()
				let tchange = new Date(changedate);
				let changetime = tchange.getTime(tchange);

				let start = new Date('2020-11-01')
				let tstart = new Date(start);
				let starttime = tstart.getTime(tstart);
				if (nowtime < changetime || changetime < starttime) {
					return false
				} else {

					return true
				}
			}
		}
	}
</script>

<style>

</style>
